package com.situ.mall.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.situ.mall.base.entity.OrderInfo;
import com.situ.mall.base.service.IOrderInfoService;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.vo.LayuiTableVO;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * ClassName: OrderController
 * Package: com.situ.mall.admin.controller
 * Description:
 *
 * @Author Darling_达
 * @Create 2023-04-13 20:20
 * @Version 1.0
 */

@Controller
@RequestMapping(URLUtil.ORDER)
public class OrderController {

    @Autowired
    private IOrderInfoService orderInfoService;

    @GetMapping(URLUtil.PAGE)
    public String page() {
        return URLUtil.ORDER_HTML;
    }


    @GetMapping(URLUtil.LIST)
    @ResponseBody
    public LayuiTableVO list(@RequestParam Integer page, @RequestParam Integer limit, String keyword) {
        return orderInfoService.doList(page, limit, keyword);
    }

    @PostMapping(URLUtil.START + URLUtil.UID)
    @ResponseBody
    public void start(@PathVariable Integer id){
        orderInfoService.updateStatusById(id);
    }


    // 导出文件
    @PostMapping(URLUtil.EXPORT)
    public void export(String keyword, HttpServletResponse response) {

        List<OrderInfo> list = orderInfoService.list(new LambdaQueryWrapper<OrderInfo>().orderByDesc(OrderInfo::getCreatetime));

        try {
            // 从项目类的加载路径中找到模块
            ClassPathResource cpr = new ClassPathResource("/templates/export/order.xlsx");
            // 用流读入模板
            InputStream in = cpr.getInputStream();
            // XSSFWorkbook  是xlsx   97office之后的excel
            // HSSFWorkbook  是xlx    97之前的
            Workbook workbook = new XSSFWorkbook(in);
            Sheet sheet = workbook.getSheetAt(0);
            for (int i = 1; i < list.size(); i++) {
                Row row = sheet.createRow(i);
                Cell indexCell = row.createCell(0);
                Cell codeCell = row.createCell(1);
                Cell priceCell = row.createCell(2);
                Cell timeCell = row.createCell(3);
                Cell statusCell = row.createCell(4);

                indexCell.setCellValue(i);
                codeCell.setCellValue(list.get(i - 1).getCode());
                priceCell.setCellValue(list.get(i - 1).getPrice().toString());
                timeCell.setCellValue(list.get(i - 1).getCreatetime().toString());
                statusCell.setCellValue(list.get(i - 1).getStatus());
            }
            // 通过response写回文件
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + System.currentTimeMillis() + ".xlsx");
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出出现异常");
        }
    }


}
